//
//  ZXJPersentationManager.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/4/19.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit


class ZXJPersentationManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    ///当前是否展示
    private var isPresent = false
    var presentFrame = CGRect.zero

    // MARK:- UIViewControllerTransitioningDelegate
    //返回负责专场对象
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let pc = XPresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    //展示转场动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: ZXJPersentationManagerDidPresented), object: self)
        return self
    }
    //转场消失
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: ZXJPersentationManagerDidDismissed), object: self)
        return self
    }
    
    // MARK:- UIViewControllerContextTransitioning
    //展示和消失动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    //如何展示和消失
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresent {
            willPresentedController(transitionContext: transitionContext)
        }
        else{
            willDismissedController(transitionContext: transitionContext)
        }
        
        //        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        //
        //        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        
        //        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        //        let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
    }
    
    ///展现动画
    private func willPresentedController(transitionContext:UIViewControllerContextTransitioning){
        
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        
        transitionContext.containerView.addSubview(toView)
        //执行动画
        toView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0)
        //设置锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toView.transform = CGAffineTransform.identity
        }) { (_) in
            //自定义完，必须调用
            transitionContext.completeTransition(true)
        }
    }
    ///消失动画
    private func willDismissedController(transitionContext:UIViewControllerContextTransitioning){
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            fromView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.00001)
        }) { (_) in
            //自定义完，必须调用
            transitionContext.completeTransition(true)
        }
    }
}
